#include<iostream>
#include<assert.h>
#include <typeinfo>
namespace STL
{
    //模拟实现vector
    template<class T>
    class vector
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;

        //默认成员函数
        vector()                                           //构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {}
        vector(size_t n, const T& val)                   //构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            reserve(n);
            for (size_t i = 0; i < n; ++i)
            {
                push_back(val);
            }
        }
        vector(long n, const T& val)                   //构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            reserve(n);
            for (size_t i = 0; i < n; ++i)
            {
                push_back(val);
            }
        }  vector(int n, const T& val)                   //构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            reserve(n);
            for (int i = 0; i < n; ++i)
            {
                push_back(val);
            }
        }
        template<class InputIterator>
        vector(InputIterator first, InputIterator last)     //构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                first++;
            }
        }
        vector(const vector<T>& v)                         //拷贝构造函数
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            _start = new T[v.capacity()];
            for (size_t i = 0; i < v.size(); ++i)
            {
                _start[i] = v[i];
            }
            _finish = _start + v.size();
            _endofstorage = _start + v.capacity();
        }
        vector<T>& operator=(const vector<T>& v)           //赋值运算符重载函数
        {
            if (this != &v)
            {
                delete[]_start;
                _start = new T[v.capacity()];
                for (size_t i = 0; i < v.size(); ++i)
                {
                    _start[i] = v[i];
                }
                _finish = _start + v.size();
                _endofstorage = _start + v.capacity();
            }
        }
        ~vector()                                          //析构函数
        {
            if (_start)
            {
                delete[]_start;
                _start = nullptr;
                _finish = nullptr;
                _endofstorage = nullptr;
            }
        }

        //迭代器相关函数
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin()const
        {
            return _start;
        }
        const_iterator end()const
        {
            return _finish;
        }

        //容量和大小相关函数
        size_t size()const
        {
            return _finish - _start;
        }
        size_t capacity()const
        {
            return _endofstorage - _start;
        }
        void reserve(size_t n)
        {
            if (n > capacity())
            {
                size_t sz = size();
                T* tmp = new T[n];
                if (_start)
                {
                    for (size_t i = 0; i < sz; ++i)
                    {
                        tmp[i] = _start[i];
                    }
                    delete[]_start;
                }
                _start = tmp;
                _finish = _start + sz;
                _endofstorage = _start + n;
            }
        }
        void resize(size_t n, const T& val = T())
        {
            if (n <= size())
            {
                _finish = _start + n;
            }
            else
            {
                if (n > capacity())
                {
                    reserve(n);
                }
                while (_finish < _start + n)
                {
                    *_finish = val;
                    _finish++;
                }
            }
        }
        bool empty()
        {
            return _start == _finish;
        }

        //修改容器内相关函数
        void push_back(const T& x)
        {
            if (_finish == _endofstorage)
            {
                size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
                reserve(newcapacity);
            }
            *_finish = x;
            _finish++;
        }
        void pop_back()
        {
            assert(!empty());
            _finish--;
        }
        void insert(iterator pos, const T& x)
        {
            if (_finish == _endofstorage)
            {
                size_t len = pos - _start;
                size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
                reserve(newcapacity);
                pos = _start + len;
            }
            iterator end = _finish;
            while (end >= pos + 1)
            {
                *end = *(end - 1);
                end--;
            }
            *pos = x;
            _finish++;
        }
        iterator erase(iterator pos)
        {
       
            assert(!empty());
            iterator it = pos + 1;
            while (it != _finish)
            {
                *(it - 1) = *it;
                it++;
            }
            _finish--;
            return pos;
        }
        void swap(vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endofstorage, v._endofstorage);
        }

        //访问容器相关函数
        T& operator[](size_t i)
        {
            assert(i < size());
            return _start[i];
        }
        const T& operator[](size_t i)const
        {
            assert(i < size());
            return _start[i];
        }
    private:
        iterator _start;//指向容器头
        iterator _finish;//指向有效数据的尾
        iterator _endofstorage;//指向容器尾
    };
}
using namespace STL;
using namespace std;
void print_v(const vector<int>& vc)
{
    for (int i = 0; i < vc.size(); i++)
    {
        cout << vc[i] << " ";
    }
    cout << endl;
}
int main()
{
    vector<int> vc(5, 0);
    vector<int> vd(vc);
    print_v(vd);
    print_v(vc);
    vc.push_back(1);
    print_v(vc);
    vc.insert(vc.begin(), 1);
    print_v(vc);
    vc.erase(vc.end()-1);
    print_v(vc);
    return 0;
}